import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_train_exam_appro.dart';
import 'package:flutter_learn_vi/src/net/dio_utils.dart';
import 'package:flutter_learn_vi/src/page/common/common_dialog.dart';
import 'package:flutter_learn_vi/src/page/study/train_exam_appro_no_page.dart';
import 'package:flutter_learn_vi/src/utils/navigator_utils.dart';
import 'package:flutter_learn_vi/src/utils/toast_utils.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2021/2/18.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///我的审批列表
class TrainExamApproListItemWidget extends StatefulWidget {
  final BeanTrainExamAppro beanTrainExamAppro;

  const TrainExamApproListItemWidget({Key key, this.beanTrainExamAppro})
      : super(key: key);

  @override
  _TrainExamApproListPageState createState() => _TrainExamApproListPageState();
}

class _TrainExamApproListPageState extends State<TrainExamApproListItemWidget> {
  @override
  Widget build(BuildContext context) {
    return buildListItemWidget(widget.beanTrainExamAppro);
  }

  buildListItemWidget(BeanTrainExamAppro beanTrainExamAppro) {
    int answerTime = beanTrainExamAppro.examAnswerTime;

    int min = answerTime ~/ 60;
    int second = (answerTime % 60);
    String minStr = min > 9 ? "$min分" : "0$min分";
    String secondStr = second > 9 ? "$second秒" : "0$second秒";
    return Container(
      margin: EdgeInsets.only(top: 10),
      color: Colors.white,
      padding: EdgeInsets.all(12),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Container(
                height: 18,
                width: 4,
                decoration: BoxDecoration(
                    color: Colors.green,
                    borderRadius: BorderRadius.all(Radius.circular(2))),
              ),
              SizedBox(
                width: 6,
              ),
              Text(
                "${beanTrainExamAppro.userReamName}",
                style: TextStyle(
                  fontWeight: FontWeight.w600,
                  fontSize: 16,
                ),
              ),
            ],
          ),
          SizedBox(
            height: 6,
          ),
          Text("培训总课时 ${beanTrainExamAppro.studyCoureNumber}"),
          Text("学员签到次数：${beanTrainExamAppro.signNumber}次"),
          SizedBox(
            height: 6,
          ),
          Text("考试时间 ${beanTrainExamAppro.examCreateTime}"),
          Text("考试用时：$minStr$secondStr"),
          Text("考试得分 ${beanTrainExamAppro.examObtainScore ~/ 10} 分"),
          SizedBox(
            height: 6,
          ),
          Text("申请时间 ${beanTrainExamAppro.createTime}"),
          buildRow()
        ],
      ),
    );
  }

  BeanTrainExamAppro _beanTrainExamAppro;

  Widget buildRow() {
    if (_beanTrainExamAppro == null) {
      _beanTrainExamAppro = widget.beanTrainExamAppro;
    }

    //审批状态 0待审批  1通过 2拒绝  3证书下发 4删除
    int statues = _beanTrainExamAppro.approStatues;

    if (statues != 0) {
      String message = "";
      switch (statues) {
        case 1:
          message = "已审批 合格";
          break;
        case 2:
          message = "已审批 不合格";
          break;
        case 3:
          message = "已审批 证书下发";
          break;
      }
      return Row(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          TextButton(
            child: Text(message),
            onPressed: () {},
          ),
        ],
      );
    }
    return Row(
      mainAxisAlignment: MainAxisAlignment.end,
      children: [
        ElevatedButton(
          child: Text('不合格'),
          onPressed: () {
            NavigatorUtils.pushPage(context,
                TrainExamApproNoPassWidget(id: widget.beanTrainExamAppro.id),
                callback: (value) {
              if (value != null) {
                _beanTrainExamAppro = value;
                setState(() {});
              }
            });
          },
        ),
        SizedBox(
          width: 10,
        ),
        ElevatedButton(
          child: Text('合格'),
          onPressed: () {
            commitTrainApproFunction("确认审批为合格吗？？？", 1);
          },
        ),
      ],
    );
  }

  void commitTrainApproFunction(String message, int statues) {
    showCommonAlertDialog(
        context: context,
        headerTitle: "温馨提示",
        contentMessag: message,
        selectText: "提交",
        cancleText: "取消",
        selectCallBack: () async {
          _commitTrainAppro(statues, message);
        });
  }

  Future<BeanTrainExamAppro> _commitTrainAppro(
      int statues, String approRemark) async {
    Map<String, dynamic> map = new Map();
    map["id"] = widget.beanTrainExamAppro.id;
    map["approStatues"] = statues;
    map["approRemark"] = "合格";

    ResponseInfo statisRresponseInfo = await DioUtils.instance
        .postRequest(url: HttpHelper.trainExamApproUpdateUrl, jsonMap: map);
    if (statisRresponseInfo.success) {
      _beanTrainExamAppro =
          BeanTrainExamAppro.fromMap(statisRresponseInfo.data);
      setState(() {});
      return null;
    } else {
      ToastUtils.showToast(statisRresponseInfo.message);
      return null;
    }
  }
}
